import {ref} from 'vue';
import apiOrder from '@/util/network_api/apiOrder.js';

/**
 * 获取工单结单状态
 */
export const useGetOrderEndStatus = () => {
	// 结单状态：true-已结单，false-待结单
	const endStatus = ref(false);

	/**
	 * 获取结单状态
	 * @param {string} orderId - 工单id
	 */
	const getEndStatus = async (orderId: string) => {
		const res = await apiOrder.getOrderEndStatus(orderId);
		if (res.code === 20000) {
			endStatus.value = res.data;
			return '';
		}
		return res.message;
	};

	return {
		endStatus,
		getEndStatus,
	};
};

/**
 * 获取结单模板
 */
export const useGetOrderEndTemplate = () => {
	// 结单模板
	const endTemplate = ref<OrderEndTemplate>({
		custAccId: '',
		objects: [],
		workId: '',
	});

	/**
	 * 获取模板
	 * @param {string} orderId - 工单id
	 */
	const getEndTemplate = async (orderId: string) => {
		const res = await apiOrder.getEndTemplate(orderId);
		if (res.code === 20000) {
			endTemplate.value = res.data;
			return '';
		}
		return res.message;
	};

	/**
	 * 切换模板
	 * @param {string} moduleId - 模块id
	 * @param {string} orderId - 工单id
	 */
	const switchTemplate = async (moduleId: string, orderId: string) => {
		const res = await apiOrder.switchEndTemplate(moduleId, orderId);
		if (res.code === 20000) {
			endTemplate.value = res.data;
			return '';
		}
		return res.message;
	};

	return {
		endTemplate,
		getEndTemplate,
		switchTemplate,
	};
};

/**
 * 获取结单信息
 */
export const useGetEndInfo = () => {
	// 结单信息
	const endInfo = ref<OrderEndInfo>({
		content: '',
		createTime: '',
		enableStatementVerifCode: false,
		faultReason: [],
		faultRelationVos: [],
		faultTypeRelationVos: [],
		module: '',
		moduleId: '',
		moduleType: 0,
		solution: [],
		userPhone: '',
		username: '',
		workNo: '',
	});

	/**
	 * 获取结单信息
	 * @param {string} orderId - 工单id
	 */
	const getEndInfo = async (orderId: string) => {
		const res = await apiOrder.getNetworkStatementInfo(orderId);
		if (res.code === 20000) {
			endInfo.value = res.data.row;
			return '';
		}
		return res.message;
	};

	return {
		endInfo,
		getEndInfo,
	};
};

/**
 * 获取工单所有模块
 */
export const useGetAllOrderModule = () => {
	// 模块列表
	const moduleList = ref<Array<OrderModule>>([]);

	// 获取模块列表请求参数
	const getModuleListParam = ref<NetworkOrderAPIRequest.GetModuleListParam>({
		ascriptionType: '',
		departmentId: '',
	});

	/**
	 * 获取模块列表
	 * @param {0|1|2|3|4} type - 模块类型：0-查询所有，1-安装，2-维修，3-保养，4-其他
	 */
	const getModuleList = async (type: 0 | 1 | 2 | 3 | 4) => {
		const res = await apiOrder.getOrderModuleList(
			getModuleListParam.value,
			type,
		);
		if (res.code === 20000) {
			moduleList.value = res.data.rows;
			return '';
		}
		return res.message;
	};

	return {
		moduleList,
		getModuleListParam,
		getModuleList,
	};
};

/**
 * 保存结单模板
 */
export const useSaveEndTemplate = () => {
	// 保存表单
	const form = ref<Array<NetworkOrderAPIRequest.SaveEndTemplate>>([]);

	/**
	 * 保存
	 * @param {string} moduleId - 模块id
	 * @param {string} orderId - 工单id
	 */
	const save = async (moduleId: string, orderId: string) => {
		console.log(form.value);
		const res = await apiOrder.saveEndTemplate(moduleId, orderId, form.value);
		return res;
	};

	return {
		form,
		save,
	};
};
